package com.qrcode.scan.camera;

import android.content.Context;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.RelativeLayout;


/**
 * Created by leo
 * on 2020/3/12.
 */
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mSurfaceHolder;
    //是否是前置摄像头
    private boolean isFront;
    private boolean isCreate=false;
    private boolean isShowOpen=false;
    private int mflash=0;

    public CameraView(Context context) {
        this(context, null);
    }

    public CameraView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CameraView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mSurfaceHolder = getHolder();
        mSurfaceHolder.setFormat(PixelFormat.TRANSPARENT);
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //当前得到的最适当的尺寸
        int rightWith = CameraInterface.getInstance(getContext()).getRightHeight();
        int rightHeight = CameraInterface.getInstance(getContext()).getRightWith();

        //当前屏幕尺寸
        int phoneWith = UIUtil.getWidth(getContext());
        int phoneHeight = UIUtil.getHeight(getContext());
        //int trueHeight = phoneWith * rightHeight / rightWith;
        Log.e("surfaceCreated","phoneWith:"+phoneWith);
        Log.e("surfaceCreated","phoneHeight:"+phoneHeight);
        //为了适配全面屏不变形做的处理
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
        //layoutParams.width = phoneHeight;
        //layoutParams.height = trueHeight;
        layoutParams.width = phoneWith;
        layoutParams.height = phoneHeight;
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        setLayoutParams(layoutParams);
        isCreate=true;
        /*if(CameraInterface.getInstance(getContext()).getmCamera()!=null) {
            CameraInterface.getInstance(getContext()).doOpenCamera(isFront, this);
        }*/
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        CameraInterface.getInstance(getContext()).doStartPreview(holder, width, height);
        if(isShowOpen){
            CameraInterface.getInstance(getContext()).doStopCamera();
            CameraInterface.getInstance(getContext()).doOpenCamera(isFront,this);
            CameraInterface.getInstance(getContext()).switchFlash(mflash);
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        CameraInterface.getInstance(getContext()).doStopCamera();
    }


    //开始拍照
    public void takePicture(OnCaptureData callback) {
        CameraInterface.getInstance(getContext()).takePicture(callback);
    }

    //重新打开摄像头
    public void restartPreview() {
        CameraInterface.getInstance(getContext()).restartPreview();
    }

    //切换摄像头
    public void switchCamera() {
        isFront = !isFront;
        if(!isCreate){
            isShowOpen=true;
            return;
        }
        CameraInterface.getInstance(getContext()).doStopCamera();
        CameraInterface.getInstance(getContext()).doOpenCamera(isFront,this);
        //CameraInterface.getInstance(getContext()).doStartPreview(mSurfaceHolder, this.getMeasuredWidth(), this.getMeasuredHeight());
    }

    public void initCamera(){
        isFront=false;
        if(!isCreate){
            isShowOpen=true;
            return;
        }
        CameraInterface.getInstance(getContext()).doStopCamera();
        CameraInterface.getInstance(getContext()).doOpenCamera(isFront,this);
    }

    public void reStartCamera(){
        //开始预览
        CameraInterface.getInstance(getContext()).reStartCamera();
    }

    public void stopPreview(){
        //停止预览
        CameraInterface.getInstance(getContext()).doStopCamera();
    }


    /**
     * 切换后置摄像头
     */
    public void resetSwitchCamera(){
        isFront=true;
        switchCamera();
    }

    public boolean isFront() {
        return isFront;
    }


    //切换闪光灯模式
    public void switchFlash(int flash) {
        mflash=flash;
        CameraInterface.getInstance(getContext()).switchFlash(flash);
    }


    //是否准备好了拍摄
    public boolean prepareVideo() {
        return CameraInterface.getInstance(getContext()).prepareVideoRecorder(mSurfaceHolder);
    }

    public void startRecord() {
        CameraInterface.getInstance(getContext()).startRecord();
    }

    public String stopRecod() {
        String videoPath = CameraInterface.getInstance(getContext()).stopRecod();
        CameraInterface.getInstance(getContext()).releaseMediaRecorder();
        return videoPath;
    }

    public String stopRecodTest() {
        String videoPath = CameraInterface.getInstance(getContext()).stopRecod();
        return videoPath;
    }

    public void releaseMediaRecorder() {
        CameraInterface.getInstance(getContext()).releaseMediaRecorder();
    }


}
